<?php

class CasesModel extends Cases{
	
	public function insertCases($arr){
		try {
			$obj = new Cases();			
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('cases',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateCases($arr){
		try {
			$obj = new Cases();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$valor = "'".$valor."'";
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Update('cases',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteCases($id){
		try {
			$db = new Db;
			$db->Delete('cases',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarCases($parametros){
		try {
			$campos = array('*');
			$where = 'idcases = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('cases',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function dropdownCases($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'idcases DESC';
			$db = new Db;
			$db->Select('cases',$campos,$where,$order);
			$view = new CasesView();
			$this->lista = $view->select($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarCases($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'p.idcases DESC';
			$db = new Db;
			$db->Select('cases AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias',$campos,$where,$order);
			$view = new CasesView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function destaquesCases($parametros){
		try {
			$campos = array('*');
			if($parametros > 0){
				$where = 'p.idcategorias = '.$parametros.' AND g.destaque = 1 GROUP BY p.idcases';
			}else{
				$where = 'p.destaque = 1 AND g.destaque = 1 GROUP BY p.idcases';
			}
			$order = 'p.ordem ASC';
			$db = new Db;
			$db->Select('cases AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias INNER JOIN imagens_cases AS g ON g.idcases = p.idcases',$campos,$where,$order);
			$view = new CasesView();
			if($parametros > 0){
				$this->lista = $view->selecionados($db->res);
			}else{
				$this->lista = $view->destaques($db->res);
			}
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function capaCases($parametros){
		try {
			$campos = array('*');
			$where = 'p.capa = 1 AND g.destaque = 1 GROUP BY p.idcases';
			$order = 'p.ordem ASC LIMIT 1';
			$db = new Db;
			$db->Select('cases AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias INNER JOIN imagens_cases AS g ON g.idcases = p.idcases',$campos,$where,$order);
			$view = new CasesView();
			$this->lista = $view->capa($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function xmlCases($parametros){
		try {
			$campos = array('*','c.nome AS nome_cat','p.titulo AS nome_case');
			$where = '(';
			foreach($parametros as $key=>$val){
				foreach($parametros[$key] as $val){
					$where .= 'idcases = '.$val.' OR '; 
				}
			}
			$where .= ' idcases = 0)';
			$order = 'p.idcases';
			$db = new Db;
			$db->Select('cases AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias',$campos,$where,$order);
			$view = new CasesView();
			$this->lista = $view->xml($db->res,$parametros);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>